package com.cinyi.ormlite.core;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("rawtypes")
public abstract class ModelWrapper<T extends BaseModel>
{
	private IGenericDao<T> dao = null ;
	
    public ModelWrapper(){
    }
    
    /**
     * 保存对象到数据库
     */
    protected void save(T t){
        try {
            getDao().save(t);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            getDao().close();
        }
    }
    
    @SuppressWarnings({ "unchecked", "hiding" })
    protected T findOne(T t){
        List<T> list = findAll(t);
        return list.size()==0?null:list.get(0) ;
    }
    
    @SuppressWarnings({ "unchecked", "hiding" })
    protected List<T> findAll(T t){
        List<T> list = new ArrayList<T>();
        try {
            list = getDao().find(t) ;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            getDao().close();
        }
        return list ;
    }
    
    protected Integer deleteByID(String id,Class clzz){
    	try {
            return getDao().delete(id, clzz);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            getDao().close();
        }
    	return -1 ;
    }
    
    public IGenericDao<T> getDao(){
    	return dao;
    }
    
}
